package com.icinfo.cloud.provider.punish.coordination.lawjoin.service.impl;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.coordination.lawjoin.dto.EditCaseStatusDto;
import com.icinfo.cloud.provider.punish.coordination.lawjoin.service.ILawJoinService;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseStatusEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoStatusChangeRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.cloud.workflow.config.mapper.ConfigWorkFlowDetailInfoMapper;
import com.icinfo.cloud.workflow.config.model.ConfigWorkFlowDetailInfo;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class LawJoinServiceImpl implements ILawJoinService {
    @Autowired
    private CaseMainInfoMapper caseMainInfoMapper;
    @Autowired
    private ICaseMainInfoService caseMainInfoService;
    @Autowired
    private ICaseMainInfoStatusChangeRecordService caseMainInfoStatusChangeRecordService;
    @Autowired
    private ConfigWorkFlowDetailInfoMapper configWorkFlowDetailInfoMapper;

    /**
     * 根据两法回调状态修改案件状态
     * @author liyafeng
     * @date 2022/9/29
      * @param dto
     * @return {@link Map< String, Object>}
     */
    @Override
    public int  editCaseStatusByLawJoinCallBack(EditCaseStatusDto dto) {
        int num=0;
        CaseMainInfoVo caseMainInfo=caseMainInfoMapper.selectCaseMainInfo(dto.getCaseId());
        //暂停
        if(Constant.STRING_1.equals(dto.getCaseChangeStatus())){
            CaseMainInfo updateCaseMainInfo=new CaseMainInfo();
            updateCaseMainInfo.setId(dto.getCaseId());
            updateCaseMainInfo.setCaseStatus(CaseStatusEnum.SUSPEND.getCode());
            num=caseMainInfoMapper.updateByPrimaryKeySelective(updateCaseMainInfo);
            //暂停方法
            caseMainInfoStatusChangeRecordService.saveCaseSuspendRecord(dto.getCaseId());

        }else if(Constant.STRING_2.equals(dto.getCaseChangeStatus())){
            //恢复办案中
            CaseMainInfo updateCaseMainInfo=new CaseMainInfo();
            updateCaseMainInfo.setId(dto.getCaseId());
            updateCaseMainInfo.setCaseStatus(CaseStatusEnum.HANDLING_CASE.getCode());
            num=caseMainInfoMapper.updateByPrimaryKeySelective(updateCaseMainInfo);
            //恢复暂停
            caseMainInfoStatusChangeRecordService.recoverCase(dto.getCaseId());
            //撤回到调查取证阶段
            String flowCode=caseMainInfo.getCaseFlowCode();
            String flowVersion=caseMainInfo.getCaseFlowVersion();
            String caseLinkCode="";
            Example example=new Example(ConfigWorkFlowDetailInfo.class);
            example.createCriteria().andEqualTo("workFlowCode",flowCode).andEqualTo("workFlowVersion",flowVersion)
                    .andEqualTo("workStageCode","INVESTIGATION");
            example.orderBy("workLinkCode").asc();
            List<ConfigWorkFlowDetailInfo> configWorkFlowDetailInfos= configWorkFlowDetailInfoMapper.selectByExample(example);
            if(configWorkFlowDetailInfos!=null&&configWorkFlowDetailInfos.size()>0){
                caseLinkCode=configWorkFlowDetailInfos.get(0).getWorkLinkCode();
            }
            caseMainInfoService.doRecallCaseHandleLink(dto.getCaseId(),caseLinkCode);
        }else if(Constant.STRING_3.equals(dto.getCaseChangeStatus())){
            //恢复暂停
            caseMainInfoStatusChangeRecordService.recoverCase(dto.getCaseId());
            //结案(涉刑移送)
            CaseMainInfo updateCaseMainInfo=new CaseMainInfo();
            updateCaseMainInfo.setId(dto.getCaseId());
            updateCaseMainInfo.setCaseStatus(CaseStatusEnum.END_CRIMINAL_TRANSFER.getCode());
            num=caseMainInfoMapper.updateByPrimaryKeySelective(updateCaseMainInfo);
        }else if(Constant.STRING_4.equals(dto.getCaseChangeStatus())){
            //涉刑移送处理中
            CaseMainInfo updateCaseMainInfo=new CaseMainInfo();
            updateCaseMainInfo.setId(dto.getCaseId());
            updateCaseMainInfo.setCaseStatus(CaseStatusEnum.HANDLING_CRIMINAL_TRANSFER.getCode());
            num=caseMainInfoMapper.updateByPrimaryKeySelective(updateCaseMainInfo);
        }
        return num;
    }

}
